<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    @dialog-close="dialogClose"
    dialog-width="600px"
  >
    <template #searchPanel>
      <search-panel
        :addPer="'SystemDict:create'"
        :selectPer="'SystemDict:page'"
        addTitle="新增字典"
        @insert="handleDataInsert('新增字典')"
        @search="search"
        :showSearchButton="true"
        :showAddButton="true"
      >
        <el-select
          clearable
          style="width: 200px"
          v-model="form.storeId"
          placeholder="所属门店"
        >
          <el-option
            v-for="item in stores"
            :key="item.id"
            :value="item.id"
            :label="item.storeName"
          />
        </el-select>
        <el-select
          clearable
          filterable
          style="width: 200px"
          v-model="form.dictKey"
          placeholder="字典类别"
        >
          <el-option
            v-for="(item, index) in dictKeyArry"
            :key="index"
            :value="item"
            :label="item"
          />
        </el-select>
        <el-input
          v-model="form.dictValue"
          clearable
          placeholder="请输入字典值"
          style="width: 200px"
        />
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :width="160"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="编辑"
            style="cursor: pointer"
            v-permission="'SystemDict:update'"
            @click="handleDataAfferentChange(row, '编辑')"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            style="color: red; margin-left: 20px; cursor: pointer"
            v-permission="'SystemDict:delete'"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
    <template #dialog>
      <dialog-data-form
        :dialogData="dialogData"
        :dictKeyArry="dictKeyArry"
        @handleDataOperate="handleDataOperateEv"
        @dialog-close="dialogClose"
      />
    </template>
  </base-layout>
</template>
<script>
import baseMixin from '@/mixins/base'
import DialogDataForm from './components/DialogDataForm.vue'
import {
  getKey,
  dictPage,
  addDict,
  updateDict,
  getDictId,
  deleteDict,
  changeStatus,
} from '@/api/after-sale/dictionary'
import { mapGetters } from 'vuex'
import store from '@/store/index';

export default {
  components: {
    DialogDataForm,
  },
  mixins: [
    baseMixin({
      idName: 'id',
      findData: dictPage,
      dataName: ['content', 'totalRecords'],
      format (res) {
        return res.map((data) => {
          return data
        })
      },
      addData: addDict,
      findDataById: getDictId,
      updateData: updateDict,
      deleteData: deleteDict,
    }),
  ],
  data () {
    return {
      form: {
        storeId: '', //门店
        dictValue: '', //字典类型
        dictKey: '', //字典值
      },
      tableColumnAttributes: [
        {
          name: '字典所属门店',
          value: 'storeName',
          type: 'text',
        },
        {
          name: '字典类别',
          value: 'dictKey',
          type: 'text',
        },
        {
          name: '字典值',
          value: 'dictValue',
          type: 'text',
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'text',
        },
        {
          name: '字典状态',
          value: 'statusValue',
          type: 'switch',
          switchPer: 'SystemDict:status',
          valueId: 'status',
          action: (row) => {
            changeStatus(row.id).then((res) => {
              this.search()
              // if(res.success){
              //   this.$message.success('')
              // }
            })
          },
        },
      ],
      dictKeyArry: [], //字典类别
    }
  },
  computed: {
    ...mapGetters(['stores', 'storesCalue']),
  },
  watch: {
    storesCalue: function (newValue, oldValue) {
      this.form.storeId = newValue
    },
  },
  created () { },
  methods: {
    handleDataOperateEv (v1, v2) {
      store.dispatch('getDictAll'); //获取所有字典
      this.handleDataOperate(v1, v2);
    },
    // 查询
    search () {
      this.searchOptions = this.form
      this.currentPage = 1
      this.setTableData()
    },
    // 获取字典基础类别
    _getKey () {
      getKey().then((res) => {
        let { data } = res
        this.dictKeyArry = data
      })
    },
  },
  mounted () {
    this._getKey()
  },
}
</script>
